﻿//TODO:通过Alt+s纳品，焦点转移去掉。
/*
 *********************************************************************
 * 程序 : DeliveryCheck.UIEntry
 * 类名 : DeliveryEntry
 * 说明 : 录入窗体类
 * 作者 : 兰智
 * 作成 : 2012/03/01 10:13
 * 履历 : 20120319 lanzhi  增加付签显示
 *        20120319 lanzhi 纳品结果显示时的字体变为12号(原9号字)
 *        20120319 lanzhi 总字数可以修改（正解误解时都可以改）
 *        20120327 lanzhi 评审项目 #35
 *        20120328 lanzhi 评审项目 #36
 *        20120510 lanzhi BUG-01在画像抽取过程中     #98
 *        20120514 lanzhi BUG-02 在进行下载JOD过程中 #99
 *        20120619 lanzhi 功能 #148
 *        20130325 lanzhi 功能 #1229 F.编码_追加FormID项目
 *********************************************************************
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;
using NTTDATA.BEENChina.ResultCertification.Client.Entry;
using NTTDATA.BEENChina.ResultCertification.Client.Entry.Model;
using NTTDATA.BEENChina.ResultCertification.Client.Entry.Logic;
using System.Linq;
using System.Text.RegularExpressions;
using NTTDATA.BEENChina.ResultCertification.Client.Entry.ServiceEntry; 

namespace NTTDATA.BEENChina.ResultCertification.Client.Entry.UI
{
    public partial class DeliveryEntry : Form
    {
        #region  公有变量定义
        public delegate void SetUICtrlStatusDel(Judge vValue);
        
        public string CategoryID { get; set; }
        public string UserID { get; set; }
        public string CategoryName { get; set; }
        public int Timeout { get; set; }
        public EntryJob _entryjob { get; set; }
      
        #endregion

        #region  私有变量定义

        private int _JudgeAnswer { get; set; }
        private string _SelectAnswer { get; set; }
        private bool IsExit = false;
        private int[] intTime; 

        #endregion

        public DeliveryEntry()
        {
            InitializeComponent();
           
        }

        

        public void SetUICtrlStatus(Judge vValue)
        {
            _entryjob.CheckPage(vValue);
        }

        public void JudgeUpdate(Judge vValue)
        {
            _entryjob.CheckPage(vValue);
        }
        private void DeliveryEntry_Load(object sender, EventArgs e)
        {
            string sTemp = DAppConfig.EntryColor;
 
            this.Text = this.Text + "-" + DAppConfig.ServerName;
             
  
            load();
        }

        private void load()
        {           
            //Application.AddMessageFilter(new KeyBoardMessageFilter(this));
            _entryjob.Page_change += new ChangeEventHandler(_entryjob_Page_change);
            _entryjob.Page_Check += new CheckLogingEventHandler(_entryjob_Page_Check);

            if (LoginHelper.GetInstance().EntryResultFontSize > 0)
            {
                this.userControlResultAnswer.FontSize = LoginHelper.GetInstance().EntryResultFontSize;
            }

            this.axBUICtrl1.Announce = "";

            this.axBUICtrl1.RestTime = this._entryjob.strShowTime(1,0,0); 

            _entryjob_Page_change();
            this.intTime = this._entryjob.intGetTestTime(this.Timeout);
            //this.JobTimer.Start();

            //上次关闭后屏幕位置
            if (LoginHelper.GetInstance().FormEntryPoint.X != 0 && LoginHelper.GetInstance().FormEntryPoint.Y != 0)
            {
                this.Location = LoginHelper.GetInstance().FormEntryPoint;
            }


            this.radioLeaveOut.Click += new EventHandler(radioMissType_Click);
            this.radioNotEnough.Click += new EventHandler(radioMissType_Click);
            this.radioRuleError.Click += new EventHandler(radioMissType_Click);
            this.radioOther.Click += new EventHandler(radioMissType_Click);

            //this.Activated += new EventHandler(DeliveryEntry_Activated);

          
        }

        //void DeliveryEntry_Activated(object sender, EventArgs e)
        //{
        //    this.userControlResultAnswer.Init();
        //}

       

        /// <summary>
        /// 检查登入按纽是否可操作状态
        /// </summary>
        private void _entryjob_Page_Check(Judge judge)
        {
            //this.btnLogin.Enabled = (judge == Judge.F1);
            this.txtError.Enabled = (judge == Judge.F2);

           

            if (judge == Judge.F1)//正解
            {
                this.txtError.Text = "";
                this._SelectAnswer = ""; 
                #region  评审项目 #35     
                this.btnJudgeSuccess.BackColor = Color.FromArgb(125, 150, 255);
                btnJudgeSuccess.ForeColor = Color.White;

                this.butJudgeError.BackColor = Color.White; //Color.FromArgb(212, 208, 200);
                this.butJudgeError.ForeColor = Color.FromArgb(255, 111, 111);

                #endregion
                this._JudgeAnswer = (int)Judge.F1;
                this.btnLogin.Focus();
                this.txtComment.Text = "";
                this.txtError.Text = "0";

                F2EntryDisable();
            }
            else
            {
                if (judge == Judge.F2)//误解
                { 

                    F2EntryEnable();

                    this._JudgeAnswer = (int)Judge.F2;
                    #region 评审项目 #35
                    this.btnJudgeSuccess.BackColor = Color.White;// Color.FromArgb(212, 208, 200);
                    this.btnJudgeSuccess.ForeColor = Color.FromArgb(125, 150, 255);


                    this.butJudgeError.BackColor = Color.FromArgb(255, 111, 111);
                    butJudgeError.ForeColor = Color.White;

                    #endregion

                }
                else if (judge == Judge.NULL)//默认
                {
                    #region 评审项目 #35
                    this.btnJudgeSuccess.BackColor = Color.White;//Color.FromArgb(212, 208, 200);
                    this.btnJudgeSuccess.ForeColor = Color.FromArgb(125, 150, 255);

                    this.butJudgeError.BackColor = Color.White; //Color.FromArgb(212, 208, 200);
                    this.butJudgeError.ForeColor = Color.FromArgb(255, 111, 111);


                    ClearMissType();


                    this.txtError.Text = "0";
                    this.txtComment.Text = "";


                    #endregion
                    this._JudgeAnswer = (int)Judge.NULL;
                 
                }
                this._SelectAnswer = "";
                //this.btnLogin.Enabled = false;
            }         
        }

        private void F2EntryEnable()
        {
            this.txtComment.Enabled = true;
            this.radioRuleError.Enabled = true;
            this.radioLeaveOut.Enabled = true;
            this.radioNotEnough.Enabled = true;
            this.radioOther.Enabled = true;

            this.txtError.Enabled = true;
        }

        private void F2EntryDisable()
        {
            this.txtComment.Enabled = false;

            ClearMissType();
             
        }

        #region UPDATE窗体
        /// <summary>
        /// 窗体刷新
        /// </summary>
        private void _entryjob_Page_change()
        {            
            //翻页更新
            this.labelIndex.Text = string.Format("現件数 {0}/{1}", _entryjob.Index + 1, _entryjob.JobItem.ChildImage.Count); 

            var imageObject = _entryjob.JobItem.ChildImage[_entryjob.Index];

            //图片显示
            this.axBUICtrl1.TotalEntryCount = _entryjob.JobItem.ChildImage.Count.ToString(CultureInfo.InvariantCulture);//JOB 总数
            
            ////录力结果   
            //this.txtAnswer.Text =
            //   imageObject.OperatorEntryResults.Single(t => t.Rank == EnumRank.Final)
            //        .EntryResult;

            var opEntryResult = imageObject.OperatorEntryResults.Single(t => t.Rank == EnumRank.Final);
            this.userControlResultAnswer.Entryformat = imageObject.CategorySetting.DataFormat;            
            
            // this.userControlResultAnswer.EntryReult = getSplitResult(imageObject.CategorySetting, entryResult);
            this.userControlResultAnswer.EntryReult = opEntryResult.SplitEntryResult;

            this.userControlResultAnswer.GridItems = imageObject.CategorySetting.CategoryType == EnumCategoryType.选择 ?
                imageObject.CategorySetting.GridItems : null;
            
            this.userControlResultAnswer.Init();

            //rule
            textBoxRult.Text = imageObject.EntryRule;

            //受番ID 显示  
            this.textBoxOrderId.Text = imageObject.OrderId;
            //子画像ID显示
            this.textBoxImageId.Text = imageObject.ImageId;

            //不存在日付┃　日付：生年月日_AFS
            //检验项目显示
            this.labelTitle.Text = _entryjob.JobItem.VerfityItemName;
            this.labelTitle2.Text = string.Format("--{0}",  imageObject.CategoryName);

            //错误原因显示
            if ( imageObject.Reason !=null && !string.IsNullOrEmpty( imageObject.Reason.ErrorRanage))
            {
                labelErrorRange.Text = StringAddNewLine(imageObject.Reason.ErrorRanage , 10);
            }
            else
            {
                labelErrorRange.Text = "注釈：";
            }

            string commonText = string.Empty;
            commonText =EntryJob. GetCommnetName( imageObject.OperatorEntryResults.Single(t => t.Rank == EnumRank.Final).EntryCommon );
            this.labelComment.Text = string.IsNullOrEmpty(commonText) ? "結果" : commonText;
                

            if (File.Exists(imageObject.ImageFullPath) == true)
            {
                this.axBUICtrl1.ImgeFileName = imageObject.ImageFullPath;
            }
            else
            {
                this.axBUICtrl1.ImgeFileName = Application.StartupPath + "\\" + "UICheck.dll";
            }
            this.axBUICtrl1.LoadPicture();
            if (this._entryjob.Index == 0)
            {
                this._entryjob.ImageStartTime = DateTime.Now;
            }


            this.SetButtonEnable(_entryjob.Index != 0, _entryjob.Index != _entryjob.JobItem.ChildImage.Count - 1);
            this.axBUICtrl1.EntryCount = (_entryjob.JobItem.ChildImage.Count - _entryjob.GetLoginCount()).ToString();

            if (_entryjob.JobItem.ChildImage[_entryjob.Index].VerfityConfirmResult.Result != string.Empty)
            {
                _entryjob.CheckPage((Judge)int.Parse(_entryjob.JobItem.ChildImage[_entryjob.Index].VerfityConfirmResult.Result));

                this.txtError.Text = _entryjob.JobItem.ChildImage[_entryjob.Index].VerfityConfirmResult.MissWord.ToString();
                this.txtComment.Text = _entryjob.JobItem.ChildImage[_entryjob.Index].VerfityConfirmResult.VerfityComment ;

                EnumMissType missType = _entryjob.JobItem.ChildImage[_entryjob.Index].VerfityConfirmResult.MissType;

                
                ShowMissType(missType); 

                //if (string.IsNullOrEmpty(_entryjob.JobPage[_entryjob.Index].Answer.SelectAnswer) == false)
                //{

                //    int iTemp = Convert.ToInt32(_entryjob.JobPage[_entryjob.Index].Answer.SelectAnswer);
                //    int iIndex = 0;
                //}
            }
            else
            {
                 _entryjob.CheckPage(Judge.NULL);
            }

            //changeEntryHistory
            history.ShowEntryResult(_entryjob.JobItem.ChildImage[_entryjob.Index]);
           
            if (this.btnLogin.Enabled == true)
            {
                this.btnLogin.Focus();
            }
 
        }
        #endregion

        /// <summary>
        /// 控制按钮启用状态
        /// </summary>
        /// <param name="boolFirst">向前翻转功能两按钮启用状态</param>
        /// <param name="boolLast">向后翻转功能两按钮启用状态</param>
        public void SetButtonEnable(bool boolFirst, bool boolLast)
        {
            buttonFirst.Enabled = boolFirst;
            buttonBack.Enabled = boolFirst;
            buttonNext.Enabled = boolLast;
            buttonLast.Enabled = boolLast;
        }

        #region 程序退出操作
        private void DeliveryEntry_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                int iTemp = 0;
                if (IsExit == false)
                {
                    if (MessageBox.Show(" ジョブ入力画面を退出してよろしいでしょうか？", "メッセージ", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        iTemp = Exit();
                        if (iTemp == -9)
                        {
                            this.IsExit = true;
                            MessageBox.Show(" アプリケーションプログラムが異常を発生したので、アカウントがロックされた。" + "\n" + "もしアンロックしたら、システム管理員を連絡してください。", "メッセージ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        IsExit = false;
                        e.Cancel = true;
                    }
                }
                else
                {
                    Exit();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(" 異常を発生 " + ex.Message, "メッセージ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private int CancelJob()
        {
            int result = 0;

            result = LoginHelper.GetInstance().CancelJob();

            return result;
        }

        private int Exit()
        {
            int Result = 0;
            if (this.IsExit == true)
            {
                EndMethod();
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                int iTemp = CancelJob();
                EndMethod();
                if (iTemp > 0)
                {
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    Result = -9;
                }
            }

            return Result;
        }

        private void EndMethod()
        {
            IsExit = true;
   
            //正常退出 清空image
            LoginHelper.RemoveImage();

            if (this.JobTimer != null)
            {
                this.JobTimer.Dispose();
            }
          
            this.axBUICtrl1.ImgeFileName = "";
            this.axBUICtrl1.Dispose();
 
            this._entryjob = null;
        }

       
        #endregion

        #region 退出窗体
        private void btnEnd_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 翻页功能
        /// <summary>
        /// 第一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonFirst_Click(object sender, EventArgs e)
        {
            _entryjob.FirstPage();
        }

        /// <summary>
        /// 向前翻一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonBack_Click(object sender, EventArgs e)
        {
            _entryjob.BackPage();
        }

        /// <summary>
        /// 向后翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonNext_Click(object sender, EventArgs e)
        {
            _entryjob.NextPage();
        }

        /// <summary>
        /// 最后一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLast_Click(object sender, EventArgs e)
        {
            _entryjob.LastPage();
        }

        #endregion

        #region 未入力
        private void btnNoEntry_Click(object sender, EventArgs e)
        {
            _entryjob.SearchNoEntry();
        }
        #endregion

        #region 登录操作
        private void btnLogin_Click(object sender, EventArgs e)
        {
            _entryjob.UpdateLogin(this._JudgeAnswer, txtComment.Text, int.Parse(txtError.Text), GetMissType()); 

            if (_entryjob.SearchNoEntry() == false)
            {
                this.btnFinish.Enabled = true;
                //this.btnFinish.Focus();
            }

            //bool bolTemp = IsCheckCount();

            //if (bolTemp == false)
            //{
            //    if (MessageBox.Show("今エラー字数は総字数より大きいので、登録で構いませんか？", "メッセージ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            //    {
            //        CheckCountMethod();
            //    }
            //    else
            //    {
            //        this.txtError.Focus();
            //    }
            //}
            //else
            //{
            //    CheckCountMethod();
            //}


        }

        private void CheckCountMethod()
        {
            string sTemp = txtError.Text == string.Empty ? "0" : txtError.Text;
            //string sCount = labCount.Text == string.Empty ? "0" : labCount.Text;

            //_entryjob.UpdateLogin(this._JudgeAnswer, this._SelectAnswer, Convert.ToInt16(sCount), Convert.ToInt16(sTemp));
            this.axBUICtrl1.EntryCount = (_entryjob.JobItem.ChildImage.Count - _entryjob.GetLoginCount()).ToString();
            
            if (Convert.ToInt16(this.axBUICtrl1.EntryCount) == 0 )
            {
                this.btnFinish.Enabled = true;
                this.btnFinish.Focus();
                this.btnNoEntry.Enabled = false;
            }
            else
            {
                _entryjob.CheckPage(Judge.NULL);
            }
        }

        private bool IsCheckCount()
        {
            bool Result = true;

            if (string.IsNullOrEmpty(this.txtError.Text) == false)
            {
              
                int iError = Convert.ToInt16(this.txtError.Text);

                //if (iError > iCount)
                //{
                //    Result = false;
                //}
                //else
                //{
                //    Result = true;
                //}

            }
            else
            {
                Result = true;
            }
            return Result;
        }
        #endregion

      
        
        #region 完了功能
        private void btnFinish_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("エントリを完了しますか？", "メッセージ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    Finish();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("異常を発生 " + ex.Message, "メッセージ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void Finish()
        {
            this.JobTimer.Stop();
            //上传job
            int iTemp = _entryjob.JobUpLoad();

            if (iTemp == 0)
            {
                this.DialogResult = DialogResult.OK;
                MessageBox.Show(" アプリケーションプログラムが異常を発生したので、アカウントがロックされた。" + "\n" + "もしアンロックしたら、システム管理員を連絡してください。", "メッセージ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
            if (iTemp > 0)
            {
//                //#region  20120619 lanzhi 功能 #148
                if (MessageBox.Show("同じカテゴリを続けるかどうか", "メッセージ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                 {


                     VerfityJobItem verfityJobItem = new JobListHelper().DownLoadJob(
                         LoginHelper.GetInstance().UserID,
                         LoginHelper.GetInstance().VerfityItemId,
                         LoginHelper.GetInstance().CategoryId,
                         LoginHelper.GetInstance().OrderDate);

                     

                     if (verfityJobItem != null && verfityJobItem.ChildImage.Any())
                     {
                         //记下本次的jobid cancel job 时候使用
                         LoginHelper.GetInstance().VerfityJobId = verfityJobItem.VerfityJobID;

                         _entryjob.JobItem = verfityJobItem;
                         this.intTime = this._entryjob.intGetTestTime(this.Timeout);
                         _entryjob.Index = 0;
                         this.btnFinish.Enabled = false;
                         this.btnNoEntry.Enabled = true;
                         this.buttonFirst.Enabled = false;
                         this.buttonBack.Enabled = false;
                         this.buttonNext.Enabled = true;
                         this.buttonLast.Enabled = true;
                         //this.JobTimer.Start();
                         //labCount.Focus();

                         _entryjob_Page_change();
                     }
                     else
                     {
                         this.IsExit = true;
                         this.Close();
                     }

                 }
                else
                 {
                   this.IsExit = true;
                    this.Close();
                 }
                
            }
           // this.IsExit = true;
           // this.Close();

        }
        #endregion

        private void btnJudgeSuccess_Click(object sender, EventArgs e)
        {
            _entryjob.CheckPage(Judge.F1);
        }

        private void butJudgeError_Click(object sender, EventArgs e)
        {
            _entryjob.CheckPage(Judge.F2);
        }
 
        private bool ISNumeric(string itemValue)
        {
            return (IsRegEx("^([0-9]*[0-9]{0,3})$", itemValue));

        }
        private bool IsRegEx(string regExValue, string itemValue)
        {

            try
            {
                Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
                if (regex.IsMatch(itemValue))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
            }
        }


        #region 时间控制
        private void JobTimer_Tick(object sender, EventArgs e)
        {
            if (intTime[0] == 0 && intTime[1] == 0 && intTime[2] == 0)
            {
                this.JobTimer.Stop();
                this.JobTimer.Enabled=false;
                this.axBUICtrl1.RestTime = this._entryjob.strShowTime(intTime[0], intTime[1], intTime[2]);
                MessageBox.Show("時間になった", "メッセージ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.IsExit = true;
                this.Close();
            }
            else if (intTime[0] == 0 && intTime[1] != 0 && intTime[2] == 0)
            {
                intTime[1] = intTime[1] - 1;
                intTime[2] = 59;
            }
            else if (intTime[0] != 0 && intTime[1] == 0 && intTime[2] == 0)
            {
                intTime[0] = intTime[0] - 1;
                intTime[1] = 59;
                intTime[2] = 59;
            }
            else if (intTime[0] != 0 && intTime[1] != 0 && intTime[2] == 0)
            {
                intTime[1] = intTime[1] - 1;
                intTime[2] = 59;
            }
            else
            {
                intTime[2] = intTime[2] - 1;
            }
            if (this.JobTimer.Enabled==true)
            {
                this.axBUICtrl1.RestTime = this._entryjob.strShowTime(intTime[0], intTime[1], intTime[2]);
            }
        }
        #endregion

        private void txtAnswer_Enter(object sender, EventArgs e)
        {
            this.btnLogin.Focus();
        }
        private void numText(TextBox NumText)
        {
            if (ISNumeric(NumText.Text))
            {

            }
            else if (NumText.Text.Length > 0)
            {
                NumText.Text = NumText.Text.Substring(0, NumText.Text.Length - 1);
                NumText.SelectionLength = 0;
                NumText.SelectionStart = int.MaxValue;
                this.btnLogin.Focus();
            }
            else
                NumText.Text = "";

        }
        private void txtError_TextChanged(object sender, EventArgs e)
        {
            numText(this.txtError);
            //if (string.IsNullOrEmpty(this._entryjob.JobPage[this._entryjob.Index].Answer.SelectAnswer) == false)
            //{
            //    if (btnLogin.Enabled == false)
            //    {
            //        btnLogin.Enabled = true;
            //    }
            //}
        }
        private void txtError_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnLogin.Focus();
            }
        }

        private void DeliveryEntry_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case (Keys.Alt | Keys.L):
                    buttonHistory_Click(null, null);
                    break;
                case(Keys.Alt|Keys.Left):
                    buttonBack_Click(null, null);
                    break;
                case(Keys.Alt|Keys.Right):
                    buttonNext_Click(null, null);
                    break;

                case (Keys.Control | Keys.Insert):
                    this.IfontPlus();
                    break;
                case (Keys.Control | Keys.Home):
                    this.IfontMinus();
                    break;

                case(Keys.Alt|Keys.Insert):
                    this.axBUICtrl1.IpicPlus();
                    //this.IfontPlus();
                    break;
                case (Keys.Alt|Keys.Home):
                    this.axBUICtrl1.IpicMinus();
                    //this.IfontMinus();
                    break;
                case (Keys.Alt | Keys.PageUp):
                    this.axBUICtrl1.IchkSize();
                    break;
                case (Keys.Alt | Keys.End):
                    this.axBUICtrl1.IpicRotateLeft();
                    break;
                case (Keys.Alt | Keys.Delete):
                    this.axBUICtrl1.IpicRotateRight();
                    break;
                case (Keys.Alt | Keys.PageDown):
                    this.axBUICtrl1.IchkRotate();
                    break;
                case (Keys.Control | Keys.Oemplus):
                    //Oemplus, Control
                    if (this.WindowState == FormWindowState.Maximized)
                    {
                        this.WindowState = FormWindowState.Normal;
                        
                    }
                    else
                    {
                        this.WindowState = FormWindowState.Maximized;
                        
                    }

                  
                    this.axBUICtrl1.IpicMinus();
                    this.axBUICtrl1.IpicPlus();
                    ChangeControlSize();
                    break;

 
                    
                case ( Keys.Escape):
                    hideHistory();
                    break;

                case (Keys.F1):
                    JudgeUpdate(Judge.F1);
                    break;

                case (Keys.F2):
                    JudgeUpdate(Judge.F2);
                    break;

                case (Keys.Alt | Keys.D1):
                    ShowMissType((EnumMissType)1);
                    break;

                case (Keys.Alt | Keys.D2):
                    ShowMissType((EnumMissType)2);
                    break;
                case (Keys.Alt | Keys.D3):
                    ShowMissType( (EnumMissType)3);
                    break;
                case (Keys.Alt | Keys.D4):
                    ShowMissType((EnumMissType)4);
                    break;
                case (Keys.Control | Keys.Tab):
                    ShowHideRule();
                    break;
                default:
                    Console.WriteLine(e.KeyData);
                    break;
 
            }
        }

        private void buttonHistory_Click(object sender, EventArgs e)
        {

           if (history == null)
             history = new EntryHistory();
               
            history.Show(); 

            //this.Activate();



        }

        private void hideHistory()
        {
            if (history != null)
                history.Hide();
        }

        private EntryHistory history = new EntryHistory();

        #region  窗体移动

        // 窗体的屏幕坐标
        Point formPoint;
        // 鼠标光标的屏幕坐标
        Point mousePoint;

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (Control.MouseButtons == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                this.Location = this.formPoint + (Size)mousePos - (Size)this.mousePoint;

                LoginHelper.GetInstance().FormEntryPoint = this.Location;
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            formPoint = this.Location;
            mousePoint = Control.MousePosition;
        }

        #endregion

        private void DeliveryEntry_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.history != null)
            {
                this.history.Close();
            }
        }

        void radioMissType_Click(object sender, EventArgs e)
        {
            RadioButton radio = (RadioButton)sender;

            if (this._JudgeAnswer != (int)Judge.F2)
            {
                radio.Checked = false;
                
                return;
            }
            else
            {
                ClearMissType();    
            }
            

            string missTypeValue = radio.Tag.ToString();

            int missType;

            int.TryParse(missTypeValue, out missType);

            if (missType > 0)
            {
                ShowMissType((EnumMissType)missType);
            }
        }

        private void ClearMissType()
        {
            radioLeaveOut.Checked = false;
            radioNotEnough.Checked = false;
            radioOther.Checked = false;
            radioRuleError.Checked = false;


            radioLeaveOut.BackColor = Color.White;
            radioNotEnough.BackColor = Color.White;
            radioOther.BackColor = Color.White;
            radioRuleError.BackColor = Color.White;

            radioLeaveOut.ForeColor = Color.Red;
            radioNotEnough.ForeColor = Color.Red;
            radioOther.ForeColor = Color.Red;
            radioRuleError.ForeColor = Color.Red;
        }

        private void ShowMissType(EnumMissType missType)
        {
           if( this._JudgeAnswer != (int)Judge.F2)
           {
               return;
           }
           else
           {
               ClearMissType();
           }

            switch (missType)
            {
                case EnumMissType.ルール誤認:
                    radioRuleError.Checked = true;
                    radioRuleError.BackColor = Color.FromArgb(255,111,111);
                    radioRuleError.ForeColor = Color.White;
                    break;
                case EnumMissType.漏れ余分:
                    radioLeaveOut.Checked = true;
                    radioLeaveOut.BackColor = Color.FromArgb(255, 111, 111);
                    radioLeaveOut.ForeColor = Color.White;
                    break;
                case EnumMissType.確認不足:
                    radioNotEnough.Checked = true;
                    radioNotEnough.BackColor = Color.FromArgb(255, 111, 111);
                    radioNotEnough.ForeColor = Color.White;
                    break;
                case EnumMissType.その他:
                    radioOther.Checked = true;
                    radioOther.BackColor = Color.FromArgb(255, 111, 111);
                    radioOther.ForeColor = Color.White;
                    break;
                case EnumMissType.None:
                    ClearMissType();
                    break;
            }
        }

        private EnumMissType GetMissType()
        {
            EnumMissType missType = EnumMissType.None;

            if (radioRuleError.Checked)
                missType = EnumMissType.ルール誤認;

            if (radioLeaveOut.Checked)
                missType = EnumMissType.漏れ余分;

            if (radioNotEnough.Checked)
                missType = EnumMissType.確認不足;

            if (radioOther.Checked)
                missType = EnumMissType.その他;


            return missType;

        }

        void ChangeControlSize()
        {
           if (this.WindowState == FormWindowState.Maximized)
           {
              /* this.panel2.Location = new System.Drawing.Point(9, 92);
               this.panel2.Height += 100;
               this.panel2.Width = this.Width - 18;

               this.panelRule.Location = new System.Drawing.Point(9, this.panel2.Location.Y + this.panel2.Height + 2);
               //this.panelRule.Height += 30;
               this.panelRule.Width = this.Width - 18;

               this.label11.Location = new System.Drawing.Point(0, 0);
               //this.label11.Height += 30;

               this.textBoxRult.Width = this.panelRule.Width - 190;

               this.panel1.Location = new System.Drawing.Point(9, this.panelRule.Location.Y + this.panelRule.Height + 2);
               this.panel1.Height = this.panel3.Location.Y - this.panel1.Location.Y - 4;
               this.panel1.Width = panel2.Width;

               this.label1.Location = new System.Drawing.Point(0, 0);
               this.label1.Height = this.panel1.Height;
               this.label4.Location = new Point(label4.Location.X, this.label1.Location.Y  + (label1.Height / 2));*/

               int imageHeight = Convert.ToInt16( this.Height * 0.5 );

               this.panelImage.Location = new System.Drawing.Point(9, 92);
               this.panelImage.Height = imageHeight;
               this.panelImage.Width = this.Width - 18;

               this.panelRule.Location = this.panelImage.Location;
               this.panelRule.Size = this.panelImage.Size;

               this.label11.Location = new System.Drawing.Point(0, 0);
               this.label11.Height = panelRule.Height;
               //this.textBoxRult.Width = this.panelRule.Width - 190;


               this.panel1.Location = new System.Drawing.Point(9, this.panelRule.Location.Y + this.panelRule.Height + 2);
               this.panel1.Height = this.panel3.Location.Y - this.panel1.Location.Y - 4;
               this.panel1.Width = panelImage.Width;

               this.label1.Location = new System.Drawing.Point(0, 0);
               this.label1.MinimumSize = new Size(0,panel1.Height);

               this.label4.Location = new Point(label4.Location.X, (panel1.Height - label4.Height )/2 );

               this.labelErrorRange.Location = new Point(this.labelComment.Location.X,
                                                         labelComment.Location.Y + labelComment.Height + 8);
               this.labelErrorRange.MinimumSize = new Size(labelErrorRange.Width, label7.Location.Y  - labelComment.Location.Y - labelComment.Height - 16);


           }

           else
           {
               this.panel1.Location = new System.Drawing.Point(9, 358);
               this.panel1.Size = new System.Drawing.Size(890, 218);

               this.panelImage.Location = new System.Drawing.Point(9, 92);
               this.panelImage.Size = new System.Drawing.Size(890, 260);

               this.panelRule.Location = new System.Drawing.Point(9, 94);
               this.panelRule.Size = new System.Drawing.Size(890, 261);

               this.label1.Location = new System.Drawing.Point(0, 0);
               this.label1.MinimumSize = new Size(0, panel1.Height);


               this.labelErrorRange.Location = new System.Drawing.Point(757, 59);
               this.labelErrorRange.MinimumSize = new System.Drawing.Size(120, 104);

           }
        }      

        private void IfontPlus()
        {
            this.userControlResultAnswer.FontSize ++;
            this.userControlResultAnswer.Init();
            LoginHelper.GetInstance().EntryResultFontSize = this.userControlResultAnswer.FontSize;
        }
        private void IfontMinus()
        {
            this.userControlResultAnswer.FontSize --;
            this.userControlResultAnswer.Init();
            LoginHelper.GetInstance().EntryResultFontSize = this.userControlResultAnswer.FontSize;

        }

        private void ShowHideRule()
        {
            if (panelImage.Visible)
            {
                panelImage.Visible = false;
                panelRule.Visible = true;

            }
            else
            {
                panelImage.Visible = true;
                panelRule.Visible = false;
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Activate();
        }

        public static string StringAddNewLine(string resultString, int length)
        {

            var temp = resultString.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
            var result = new StringBuilder();

            foreach (string item in temp)
            {
                int starIndex = 0;

                do
                {

                    string tempVal;

                    if (starIndex + length <= item.Length)
                    {
                        tempVal = item.Substring(starIndex, length);
                    }
                    else
                    {
                        tempVal = item.Substring(starIndex);
                    }

                    result.Append(tempVal);
                    result.Append(Environment.NewLine);

                    starIndex += length;

                } while (starIndex < item.Length);

            }

            return result.ToString();
        }
    }
}
